﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

/// <summary>
/// Documents 的摘要说明
/// </summary>
namespace Housing
{
    public class Documents
    {
        private int _id;
        private string _name;
        private string _type;
        private string _remark;

        public Documents()
        {
        }

        public Documents(int docid)
        {
            GetDocumentById(docid);
        }

        public int Doc_id
        {
            get { return _id; }
            set { _id = value; }
        }

        public string Doc_name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string Doc_type
        {
            get { return _type; }
            set { _type = value; }
        }

        public string Doc_remark
        {
            get { return _remark; }
            set { _remark = value; }
        }

        protected string connString = SqlHelper.ConnectionString;

        public void GetDocumentById(int docid)
        {
            string query = "SELECT * FROM Documents WHERE doc_id=" + docid;
            SqlDataReader dr = SqlHelper.ExecuteReader(connString,CommandType.Text,query);
            if (dr.Read())
            {
                Doc_name = Utils.GetString(dr[1]);
                Doc_type = Utils.GetString(dr[2]);
                Doc_remark = Utils.GetString(dr[3]);
            }
            dr.Close();
        }

        public bool AddNewDocument(Documents document)
        {
            string query = @"INSERT INTO Documents(doc_name,doc_type ,doc_remark) VALUES('" + document.Doc_name + "','" + document.Doc_type + "','" + document.Doc_remark + "')";
            bool result = Convert.ToBoolean(SqlHelper.ExecuteNonQuery(connString, CommandType.Text, query));
            return result;
        }

        public bool UpdateADocument(Documents document)
        {
            string query = "UPDATE Documents SET doc_name='" + document.Doc_name + "',doc_type='" + document.Doc_type + "',doc_remark='" + document.Doc_remark + "' WHERE doc_id="+document.Doc_id;
            bool result = Convert.ToBoolean(SqlHelper.ExecuteNonQuery(connString, CommandType.Text, query));
            return result;
        }

        public SqlDataReader GetAllDocuments()
        {
            string query = "SELECT * FROM Documents";
            SqlDataReader dr = SqlHelper.ExecuteReader(connString, CommandType.Text, query);
            return dr;
        }

        public DataSet GetAllDocumentsds()
        {
            string query = "SELECT * FROM Documents";
            DataSet ds = SqlHelper.ExecuteDataset(connString, CommandType.Text, query);
            return ds;
        }

        public bool DeleteADocument(int id)
        {
            string query = "DELETE FROM Documents WHERE doc_id=" + id;
            bool result = Convert.ToBoolean(SqlHelper.ExecuteNonQuery(connString, CommandType.Text, query));
            return result;
        }

        public string GetDocTypeByNoid(string noid)
        {
            string cmdtext = "SELECT doc_type FROM Documents WHERE doc_name IN(SELECT apply_docname FROM Apply WHERE apply_noid=" + noid + ")";
            string typename = Convert.ToString(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, CommandType.Text, cmdtext));
            return typename;
        }
    }
}